//
//  NSDictionary+WhithNSNull.m
//  常用控件的封装
//
//  Created by Dxc_iOS on 2017/6/7.
//  Copyright © 2017年 Dxc_iOS. All rights reserved.
//

#import "NSDictionary+WhithNSNull.h"

@implementation NSDictionary (WhithNSNull)
//将NSDictionary中的Null类型的项目转化成@""
+(NSDictionary *)nullDic:(NSDictionary *)dict
{
    NSArray *keyArr = [dict allKeys];
    NSMutableDictionary *resDic = [[NSMutableDictionary alloc]init];
    for (int i = 0; i < keyArr.count; i ++)
    {
        id obj = [dict objectForKey:keyArr[i]];
        
        obj = [self changeType:obj];
        
        [resDic setObject:obj forKey:keyArr[i]];
    }
    return resDic;
}

//将NSArray元素中的NSDictionary中的Null类型的项目转化成@""
+(NSArray *)nullArr:(NSArray *)array
{
    NSMutableArray *resArr = [[NSMutableArray alloc] init];
    for (int i = 0; i < array.count; i ++)
    {
        id obj = array[i];
        
        obj = [self changeType:obj];
        
        [resArr addObject:obj];
    }
    return resArr;
}

//将NSString类型的原路返回
+(NSString *)stringToString:(NSString *)string
{
    return string;
}

//将NSString为Null类型的转化成@""
+(NSString *)nullToString
{
    return @"";
}

#pragma mark - 公有方法
//类型识别:将所有的NSNull类型转化成@""
+(id)changeType:(id)object
{
    if ([object isKindOfClass:[NSDictionary class]]){
        return [self nullDic:object];
    }
    else if([object isKindOfClass:[NSArray class]]){
        return [self nullArr:object];
    }
    else if([object isKindOfClass:[NSString class]]){
        return [self stringToString:object];
    }
    else if([object isKindOfClass:[NSNull class]]){
        return [self nullToString];
    }
    else{
        return object;
    }
}
@end
